************************************************************************************************************************
* Replication Do-File for 
*
* Rudra, Nita, Irfan Nooruddin, and Niccolo W. Bonifai. 2021.
* "Globalization Backlash in Developing Countries: Broadening the Research Agenda"
* Comparative Political Studies, volume 54, issue 13 (Nov), pages 2416-2441.
* https://doi.org/10.1177%2F00104140211037575
*
* All models estimated using Stata 16.1.
* Please communicate errors identified to in62@georgetown.edu. Thanks in advance for your help.
************************************************************************************************************************

// NOTE: For convenience, change the working directory below to wherever you've saved the folder
clear // Clear Stata
*cd 


set more off
log using "replication", replace // Start a log file

*******************************************
*******************************************
*******************************************// BEGIN REPLICATION
*******************************************
*******************************************

***************************************************************************
* Figure 1 *
***************************************************************************

use "$folder/Data/RNB_Figure1.dta"

twoway (line sspta_total year, clcolor(black) c(l) yaxis(1))  ///
(line nspta_total year, clcolor(black) clpattern(dash) c(l) yaxis(1))  ///
(line nnpta_total year, clcolor(black) xtitle("") graphregion(color(white)) xlabel(1949(10)2019) clpattern(shortdash) c(l) yaxis(1)) 

***************************************************************************
* Estimating the period of liberalization using RNB measure *
***************************************************************************

use "$folder/Data/RNB_Replication_TSCS.dta"

gen yrsopen_final=year-rnb
replace yrsopen_final=. if yrsopen_final<0
gen lnyrsopen_final=ln(yrsopen_final)
gen lnyrsopen_finalXless_hs=lnyrsopen_final*low_skill

***************************************************************************
* Figure 2 *
***************************************************************************

bysort country year: egen good_trade_high3=count(trade_attitude_country) if low_skill==0 & trade_attitude_country==3
bysort country year: egen good_trade_high_4=count(trade_attitude_country) if low_skill==0 & trade_attitude_country==4
bysort country year: egen mean_good_trade3=mean(good_trade_high3)
bysort country year: egen mean_good_trade4=mean(good_trade_high_4)
bysort country year: gen good_trade_high=mean_good_trade3 + mean_good_trade4

bysort country year: egen good_trade_high_all=count(trade_attitude_country) if low_skill==0
bysort country year: egen mean_good_trade_all=mean(good_trade_high_all)
bysort country year: gen good_trade_high_good=good_trade_high/mean_good_trade_all

bysort country year: egen good_trade_low3=count(trade_attitude_country) if low_skill==1 & trade_attitude_country==3
bysort country year: egen good_trade_low_4=count(trade_attitude_country) if low_skill==1 & trade_attitude_country==4
bysort country year: egen mean_good_trade3_low=mean(good_trade_low3)
bysort country year: egen mean_good_trade4_low=mean(good_trade_low_4)
bysort country year: gen good_trade_low=mean_good_trade3_low + mean_good_trade4_low
	
bysort country year: egen good_trade_low_all=count(trade_attitude_country) if low_skill==1
bysort country year: egen mean_good_trade_all_low=mean(good_trade_low_all)
bysort country year: gen good_trade_low_good=good_trade_low/mean_good_trade_all_low

bysort country year: gen good_trade_diff=good_trade_high_good-good_trade_low_good
rename good_trade_diff trade_positive

gen year2c = substr(year_string,3,2)

egen iso_year=concat(iso3_code year2c)

graph twoway (scatter trade_positive yrsopen_final, ms(none) mlabel(iso_year) mlabsize(tiny) mlabcolor(black)) (lfit trade_positive yrsopen_final), yline(0) graphregion(color(white)) xscale(range(0 75)) xtitle("Years since liberalization") ytitle("Difference in trade support between high & low skill" "(Difference in %; scaled -1 to 1)") legend(off) xlabel(0(25)75) graphregion(color(white))

***************************************************************************
* Table A1 - Effect on Attitudes to Trade * Model 1 * 
***************************************************************************

xtmixed trade_attitude_country female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ i.year/*
*/  || /*
*/ country: , cov(unstr) mle var 

***************************************************************************
* Table A1 - Effect on Attitudes to Trade * Model 2 * 
***************************************************************************
 
xtmixed trade_attitude_country female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs i.year/*
*/  || /*
*/ country: , cov(unstr) mle var 

***************************************************************************
* Figure 3 - Effect of Low Skill on Attitudes to Trade * 
***************************************************************************

predictnl tradepred2 = _b[low_skill]+_b[lnyrsopen_finalXless_hs]*lnyrsopen_final, ci(pc1 pc2)
twoway scatter tradepred2 pc1 pc2 lnyrsopen_final if lnyrsopen_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Trade Support") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since RNB liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on trade attitudes") yaxis(1) yscale(range(0) axis(1)) graphregion(color(white))
drop tradepred2 pc1 pc2

***************************************************************************
* Table A1 - Effect on POUM * Model 3
***************************************************************************

xtmixed poum_children female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

***************************************************************************
* Table 1A - Effect on POUM * Model 4
***************************************************************************

xtmixed poum_children female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

***************************************************************************
* Figure 4 - Effect of Low Skill on POUM * 
***************************************************************************

predictnl poumpred2 = _b[low_skill]+_b[lnyrsopen_finalXless_hs]*lnyrsopen_final, ci(pc1 pc2)
scatter poumpred2 pc1 pc2 lnyrsopen_final if lnyrsopen_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Poum") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since RNB liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on Poum") graphregion(color(white))
drop poumpred2 pc1 pc2

***************************************************************************
* Robustness check using WW measure * 
***************************************************************************

***************************************************************************
* Estimating the period of liberalization using WW measure *
***************************************************************************

gen yrsww_final=year-wacziarg_welch
replace yrsww_final=. if yrsww_final<0
gen lnyrsww_final=ln(yrsww_final)
gen lnyrsww_finalXless_hs=lnyrsww_final*low_skill

***************************************************************************
* Figure A1 - Effect on Attitudes to Trade * 
***************************************************************************

xtmixed trade_attitude_country female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsww_final /*
*/ lnyrsww_finalXless_hs i.year/*
*/  || /*
*/ country: , cov(unstr) mle var

predictnl tradepred2 = _b[low_skill]+_b[lnyrsww_finalXless_hs]*lnyrsww_final, ci(pc1 pc2)
twoway scatter tradepred2 pc1 pc2 lnyrsww_final if lnyrsww_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Trade Support") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since WW liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on trade attitudes") yaxis(1) yscale(range(0) axis(1)) graphregion(color(white))
drop tradepred2 pc1 pc2

***************************************************************************
* Figure A2 - Effect on POUM * 
***************************************************************************

xtmixed poum_children female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsww_final /*
*/ lnyrsww_finalXless_hs i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

predictnl poumpred2 = _b[low_skill]+_b[lnyrsww_finalXless_hs]*lnyrsww_final, ci(pc1 pc2)
scatter poumpred2 pc1 pc2 lnyrsww_final if lnyrsww_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Poum") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since WW liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on Poum") graphregion(color(white))
drop poumpred2 pc1 pc2

***************************************************************************
* Robustness check using double shock measure * 
***************************************************************************

gen yrsdouble_final=year-double_shock
replace yrsdouble_final=. if yrsdouble_final<0
gen lnyrsdouble_final=ln(yrsdouble_final)
gen lnyrsdouble_finalXless_hs=lnyrsdouble_final*low_skill

***************************************************************************
* Figure A3 - Effect on Attitudes to Trade * 
***************************************************************************

xtmixed trade_attitude_country female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsdouble_final /*
*/ lnyrsdouble_finalXless_hs i.year/*
*/  || /*
*/ country: , cov(unstr) mle var

predictnl tradepred2 = _b[low_skill]+_b[lnyrsdouble_finalXless_hs]*lnyrsdouble_final, ci(pc1 pc2)
twoway scatter tradepred2 pc1 pc2 lnyrsdouble_final if lnyrsdouble_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Trade Support") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since double shock liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on trade attitudes") yaxis(1) yscale(range(0) axis(1)) graphregion(color(white))
drop tradepred2 pc1 pc2

***************************************************************************
* Figure A4 - Effect on POUM * 
***************************************************************************

xtmixed poum_children female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsdouble_final /*
*/ lnyrsdouble_finalXless_hs i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

predictnl poumpred2 = _b[low_skill]+_b[lnyrsdouble_finalXless_hs]*lnyrsdouble_final, ci(pc1 pc2)
scatter poumpred2 pc1 pc2 lnyrsdouble_final if lnyrsdouble_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Poum") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since double shock liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on Poum") graphregion(color(white))
drop poumpred2 pc1 pc2

***************************************************************************
* Robustness check using universal measure of skill * 
***************************************************************************

gen lnyrsopen_finalXless_hs2=lnyrsopen_final*low_skill2

***************************************************************************
* Table A2 Models 1-6 * 
***************************************************************************

xtmixed trade_attitude_country female working_age low_skill2  /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs2 i.year/*
*/  || /*
*/ country: , cov(unstr) mle var

xtmixed trade_attitude_country female working_age imf_participation low_skill  /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs i.year/*
*/  || /*
*/ country: , cov(unstr) mle var

xtmixed trade_attitude_country female working_age imf_participation low_skill2  /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs2 i.year/*
*/  || /*
*/ country: , cov(unstr) mle var

xtmixed poum_children female working_age low_skill2 /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs2 i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

xtmixed poum_children female working_age imf_participation low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

xtmixed poum_children female working_age imf_participation low_skill2 /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs2 i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

***************************************************************************
* Models not featured in paper and Appendix * 
***************************************************************************

***************************************************************************
* Leading year of liberalization (RNB) by 5 years * 
***************************************************************************

gen yrsopen_final_5=yrsopen_final+5 
replace yrsopen_final_5=. if yrsopen_final_5<0
gen lnyrsopen_final_5=ln(yrsopen_final_5)
gen lnyrsopen_finalXless_hs_5=lnyrsopen_final_5*low_skill

xtmixed trade_attitude_country female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final_5 /*
*/ lnyrsopen_finalXless_hs_5 i.year/*
*/  || /*
*/ country: , cov(unstr) mle var 

predictnl tradepred2 = _b[low_skill]+_b[lnyrsopen_finalXless_hs_5]*lnyrsopen_final_5, ci(pc1 pc2)
twoway scatter tradepred2 pc1 pc2 lnyrsopen_final_5 if lnyrsopen_final_5>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Trade Support") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since RNB liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on trade attitudes") yaxis(1) yscale(range(0) axis(1)) graphregion(color(white))
drop tradepred2 pc1 pc2

xtmixed poum_children female employed married working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final_5 /*
*/ lnyrsopen_finalXless_hs_5 i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

predictnl poumpred2 = _b[low_skill]+_b[lnyrsopen_finalXless_hs_5]*lnyrsopen_final_5, ci(pc1 pc2)
scatter poumpred2 pc1 pc2 lnyrsopen_final if lnyrsopen_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Poum") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since RNB liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on Poum") graphregion(color(white))
drop poumpred2 pc1 pc2

***************************************************************************
* Models including employment and marital status (excludes 2018 data) * 
***************************************************************************

xtmixed trade_attitude_country employed married female working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs i.year/*
*/  || /*
*/ country: , cov(unstr) mle var 

predictnl tradepred2 = _b[low_skill]+_b[lnyrsopen_finalXless_hs]*lnyrsopen_final, ci(pc1 pc2)
twoway scatter tradepred2 pc1 pc2 lnyrsopen_final if lnyrsopen_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Trade Support") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since RNB liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on trade attitudes") yaxis(1) yscale(range(0) axis(1)) graphregion(color(white))
drop tradepred2 pc1 pc2

xtmixed poum_children female employed married working_age low_skill /* 
*/ lngdppc gdppc_growth trade_gdp lnyrsopen_final /*
*/ lnyrsopen_finalXless_hs i.year /*
*/  || /*
*/ country: , cov(unstr) mle var

predictnl poumpred2 = _b[low_skill]+_b[lnyrsopen_finalXless_hs]*lnyrsopen_final, ci(pc1 pc2)
scatter poumpred2 pc1 pc2 lnyrsopen_final if lnyrsopen_final>=0, /*
*/ sort ytitle("Estimated Effect of Low Education on Poum") leg(off) c(l l l)  /*
*/ lpattern(solid dash dash) ms(i i i)  yline(0) /*
*/ xtitle("Years since RNB liberalization (logged)") /*
*/ ytitle("Estimated effect of low-skill status on Poum") graphregion(color(white))
drop poumpred2 pc1 pc2


***************************************************************************
* Drop generated variables * 
***************************************************************************

drop yrsopen_final yrsopen_final_5 lnyrsopen_final lnyrsopen_finalXless_hs yrsww_final lnyrsww_final lnyrsww_finalXless_hs yrsdouble_final lnyrsdouble_final lnyrsdouble_finalXless_hs lnyrsopen_finalXless_hs2 yrsopen_final_5 lnyrsopen_final_5 lnyrsopen_finalXless_hs_5 iso_year good_trade_high3 good_trade_high_4 mean_good_trade3 mean_good_trade4 good_trade_high good_trade_high_all mean_good_trade_all good_trade_high_good good_trade_low3 good_trade_low_4 mean_good_trade3_low mean_good_trade4_low good_trade_low good_trade_low_all mean_good_trade_all_low good_trade_low_good trade_positive year2c

*******************************************
*******************************************
*******************************************// END OF REPLICATION
*******************************************
*******************************************

log off 
log close
